package com.example.teaching_server.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 用户登录DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "用户登录请求参数")
public class LoginDTO {

    @NotBlank(message = "用户名不能为空")
    @Size(min = 3, max = 50, message = "用户名长度在3-50字符之间")
    @Schema(description = "用户名/手机号/邮箱", example = "admin", requiredMode = Schema.RequiredMode.REQUIRED)
    private String username;

    @NotBlank(message = "密码不能为空")
    @Size(min = 6, max = 100, message = "密码长度在6-100字符之间")
    @Schema(description = "登录密码", example = "password123", requiredMode = Schema.RequiredMode.REQUIRED)
    private String password;

    /**
     * 验证是否为邮箱格式
     */
    @Schema(description = "是否为邮箱登录", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isEmailLogin() {
        return username != null && username.contains("@") && username.contains(".");
    }

    /**
     * 验证是否为手机号格式
     */
    @Schema(description = "是否为手机号登录", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isPhoneLogin() {
        return username != null && username.matches("^1[3-9]\\d{9}$");
    }

    /**
     * 获取登录类型
     */
    @Schema(description = "登录类型", accessMode = Schema.AccessMode.READ_ONLY)
    public String getLoginType() {
        if (isEmailLogin()) {
            return "EMAIL";
        } else if (isPhoneLogin()) {
            return "PHONE";
        } else {
            return "USERNAME";
        }
    }

    /**
     * 密码强度验证（基础版本）
     */
    @Schema(description = "密码强度", accessMode = Schema.AccessMode.READ_ONLY)
    public String getPasswordStrength() {
        if (password == null) return "UNKNOWN";

        if (password.length() >= 12 &&
                password.matches(".*[A-Z].*") &&
                password.matches(".*[a-z].*") &&
                password.matches(".*\\d.*") &&
                password.matches(".*[!@#$%^&*()].*")) {
            return "STRONG";
        } else if (password.length() >= 8) {
            return "MEDIUM";
        } else {
            return "WEAK";
        }
    }

    /**
     * 敏感信息脱敏
     */
    @Schema(description = "脱敏后的用户名", accessMode = Schema.AccessMode.READ_ONLY)
    public String getMaskedUsername() {
        if (username == null) return null;

        if (isEmailLogin()) {
            int atIndex = username.indexOf('@');
            if (atIndex > 3) {
                return username.substring(0, 3) + "***" + username.substring(atIndex);
            } else {
                return username.substring(0, 1) + "***" + username.substring(atIndex);
            }
        } else if (isPhoneLogin()) {
            return username.substring(0, 3) + "****" + username.substring(7);
        } else {
            if (username.length() > 2) {
                return username.substring(0, 2) + "***";
            } else {
                return username.charAt(0) + "***";
            }
        }
    }
}
